package com.citywithincity.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;

import org.apache.commons.codec.binary.Base64;





public class IoUtil {
	/**
	 * 写入base64的数据
	 * @param base64
	 * @param path
	 * @throws IOException 
	 */
	public static void writeBase64(String base64,String path) throws IOException{
		write(path,Base64.decodeBase64(base64));
	}
	
	public static void writeBase64(String base64,File file) throws IOException{
		write(file,Base64.decodeBase64(base64));
	}
	
	/**
	 * 读取base64
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readBase64(String path) throws IOException{
		return Base64.encodeBase64String(readFromFile(path));
	}
	public static byte[] readFromInputStream(InputStream is,int length) throws IOException{
		BufferedInputStream reader = null;
		try{
			reader = new BufferedInputStream(is);
			byte[] buffer = new byte[length];
			reader.read(buffer);
			return buffer;
		}finally{
			close(reader);
		}
	}
	public static byte[] readFromFile(String path) throws IOException{
		BufferedInputStream reader = null;
		try{
			File file = new File(path);
			reader = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[(int) file.length()];
			reader.read(buffer);
			return buffer;
		}finally{
			close(reader);
		}
	}
	public static byte[] readFromFile(File file) throws IOException{
		BufferedInputStream reader = null;
		try{
			reader = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[(int) file.length()];
			reader.read(buffer);
			return buffer;
		}finally{
			close(reader);
		}
	}
	
	
	
	public static String readContent(InputStream inputStream,String encoding) throws IOException{
		BufferedReader reader =null;
		try{
			reader = new BufferedReader(new InputStreamReader(inputStream,encoding));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while((line=reader.readLine())!=null){
				sb.append(line);
			}
			return sb.toString();
		}finally{
			close(reader);
		}
	}
	
	
	public static String readContent(String src) throws IOException{
		BufferedReader reader =null;
		try{
			reader = new BufferedReader(new FileReader(src));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while((line=reader.readLine())!=null){
				sb.append(line);
			}
			return sb.toString();
		}finally{
			close(reader);
		}
		
	}
	/**
	 * 写入文件
	 * @param file
	 * @param content
	 * @throws IOException 
	 */
	public static void write(String file,String content) throws IOException{
		FileWriter writer = null;
		try{
			writer = new FileWriter(new File(file));
			writer.write(content);
		}finally {
			close(writer);
		}
		
	}
	public static void close(Writer writer) {
		if(writer!=null){
			try{writer.close();}catch(IOException e){}
		}
	}

	/**
	 * 写入文件
	 * @param file
	 * @param inputStream
	 * @param len
	 * @throws IOException
	 */
	public static void write(File file,InputStream inputStream,int len) throws IOException{
		FileOutputStream writer = new FileOutputStream(file);
		byte[] buffer = new byte[4096];
		do{
			int readed = inputStream.read(buffer, 0, Math.min(len, 4096));
			writer.write(buffer,0,readed);
			len -= readed;
		}while(len>0);
		writer.close();
	}
	public static void write(File file,byte[] content) throws IOException{
		FileOutputStream writer = null;
		try{
			writer = new FileOutputStream(file);
			writer.write(content);
		}finally{
			IoUtil.close(writer);
		}
		
	}
	public static void write(File file,String content) throws IOException{
		FileWriter writer = null;
		try{
			writer = new FileWriter(file);
			writer.write(content);
		}finally {
			close(writer);
		}
	}
	public static void write(String file,byte[] content) throws IOException{
		FileOutputStream writer = null;
		try{
			writer = new FileOutputStream(file);
			writer.write(content);
		}finally{
			IoUtil.close(writer);
		}
	}
	
	/**
	 * 
	 * @param is
	 * @param os
	 * @throws IOException
	 */
	public static void copy(InputStream is,OutputStream os) throws IOException{
		byte[] buffer = new byte[1024*4];
		int len = -1;
		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);
		}
	}
	
	public static void copyAndCloseAll(InputStream is,OutputStream os) throws IOException{
		try{
			byte[] buffer = new byte[1024*4];
			int len = -1;
			while ((len = is.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}
		}finally{
			close(is);
			close(os);
		}
		
	}
	
	/**
	 * 
	 * @param reader
	 * @return
	 */
	public static String readAndClose(BufferedReader reader) throws IOException{
		try{
			String line;
		  	   StringBuilder sb = new StringBuilder();
		  	   while( ( line = reader.readLine())!=null){
		  		   sb.append(line);
		  	   }
		  	   return sb.toString();
		}finally{
			close(reader);
		}
		
	}

	public static void close(InputStream inputStream) {
		if(inputStream!=null)try{inputStream.close();}catch(IOException e){}
	}
	public static void close(Reader reader) {
		if(reader!=null)try{reader.close();}catch(IOException e){}
	}
	public static void close(OutputStream outputStream) {
		if(outputStream!=null)try{outputStream.close();}catch(IOException e){}
	}

	public static void close(PrintWriter writer) {
		if(writer!=null)writer.close();
	}
	
	/**
	 * 强制读取,充满
	 * @param bytes
	 * @param inputStream
	 * @throws IOException
	 */
	public static void read(byte[] bytes,InputStream inputStream) throws IOException{
		int total = bytes.length;
		int readed = 0;
		while(true){
			int n= inputStream.read(bytes, readed, total-readed);
			if(n<=0){
				throw new IOException();
			}
			readed += n;
			if(readed>=total){
				break;
			}
		}
	}

	public static void close(Socket socket) {
		if(socket!=null){
			try {
				socket.close();
			} catch (IOException e) {
				
			}
		}
		
	}
	
}
